// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const path = require('path');
const WebpackBar = require('webpackbar');
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer');
const SpeedMeasurePlugin = require('speed-measure-webpack-plugin');
const { addBeforeLoaders, loaderByName, removeLoaders } = require('@craco/craco');

const smp = new SpeedMeasurePlugin(); //记录loader和plugin的耗时情况

module.exports = {
  webpack: smp.wrap({
    alias: {
      "@": path.resolve(__dirname, "src"),
    },
    plugins: [
      new WebpackBar(),
      new BundleAnalyzerPlugin({
        analyzerMode: 'static',
        reportFilename:'report.html', //打包结果分析
        openAnalyzer:false
      }),
    ],
    configure: (webpackConfig,{ env,paths }) => {
      addBeforeLoaders(webpackConfig, loaderByName("style-loader"),"thread-loader");
      addBeforeLoaders(webpackConfig, loaderByName("style-loader"),"cache-loader");
      // removeLoaders(webpackConfig, loaderByName("source-map-loader"));
      // // splitChunks打包优化
      // webpackConfig.optimization.splitChunks = {
      //   ...webpackConfig.optimization.splitChunks,
      //   cacheGroups: {
      //     commons: {
      //       chunks: "all",
      //       // 将两个以上的chunk所共享的模块打包至commons组。
      //       minChunks: 2,
      //       name: "commons",
      //       priority: 80,
      //     },
      //   },
      // };
      return webpackConfig;
    }
  }),
};